﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Confucius.dao;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Windows.Resources;
using Confucius.entity;
using Confucius.utils;
using DBContent.Model;

namespace Confucius.daoImpl
{
    /// <summary>
    /// Author:Liangchuan Gu
    /// date:2012-04-08
    /// </summary>
    public class GameRuleDAOImpl : GameRuleDAO
    {
        public GameRuleDAOImpl() { }

        public List<String> getGameObjectAssociatedTriggerIDs(String gameID,String conditionType,String gameObjectID) 
        {
		    List<String> triggerIDs = new List<String>();
		    String triggerID = null;
		    String searchObjectID = null;
		    String searchConditionType = null;

		    try {
                Uri uri = new Uri("assets/gameFiles/" + gameID + "/gamecondition.xml", UriKind.Relative);
                StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
                XDocument loadedData = XDocument.Load(resourceInfo.Stream);

                var conditions = from condition in loadedData.Descendants("gamecondition")
                            select condition;

                foreach (XElement gameCondition in conditions)
                {
                    triggerID = (string)gameCondition.Element("triggerID").Value;
                    searchObjectID = (string)gameCondition.Element("paramX").Value;
                    searchConditionType = (string)gameCondition.Element("conditionType").Value;
                    // Get the relevant trigger in DB
                    if (searchObjectID.Equals(gameObjectID) && searchConditionType.Equals(conditionType))
                    {
                        DBTrigger trigger = new DBTrigger();
                        trigger.TriggerID = triggerID;
                        String triggerStatus = ConfuciusDBHelper.getTriggerStatus(trigger);

                        if (triggerStatus.Equals(Trigger.NOT_TRIGGER_STATUS))
                        {
                            triggerIDs.Add(triggerID);
                        }
                    }

                }

		    } 
            catch (Exception e) 
            {
                throw new Exception("Could not read trigger statuses from DB. Info is: " + e.Message);
		    }
		    return triggerIDs;
	    }

        public Trigger loadTrigger(String triggerID, String gameID) 
        {
            Trigger trigger = new Trigger(triggerID);

            try
            {
                Uri uri = new Uri("assets/gameFiles/" + gameID + "/gametrigger.xml", UriKind.Relative);
                StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
                XDocument loadedData = XDocument.Load(resourceInfo.Stream);

                var triggers = from record in loadedData.Descendants("gameTrigger")
                                 select record;

                foreach (XElement triggerRecord in triggers)
                {
                    string id = (string)triggerRecord.Element("triggerID").Value;
                    if (id.Equals(triggerID))
                    {
                        trigger.TriggerID = id;
                        trigger.TriggerText = (string)triggerRecord.Element("textAfterTrigger").Value;
                        trigger.ActionObjectID = (string)triggerRecord.Element("actionObjectID").Value;
                        trigger.ActionType = (string)triggerRecord.Element("actionType").Value;
                        break;
                    }
                }

                //fill in the conditions
                try
                {
                    uri = new Uri("assets/gameFiles/" + gameID + "/gamecondition.xml", UriKind.Relative);
                    resourceInfo = Application.GetResourceStream(uri);
                    loadedData = XDocument.Load(resourceInfo.Stream);

                    var conditionRecords = from record in loadedData.Descendants("gamecondition")
                                 select record;

                    foreach (XElement conditionRecord in conditionRecords)
                    {
                        String searchTriggerID = (string)conditionRecord.Element("triggerID").Value;
                        if(searchTriggerID.Equals(triggerID))
                        {
                            Condition condition = new Condition();
                            condition.ConditionType = (string)conditionRecord.Element("conditionType").Value;
                            condition.ParamX = (string)conditionRecord.Element("paramX").Value;
                            condition.ParamY = (string)conditionRecord.Element("paramY").Value;
                            trigger.Conditions.Add(condition);
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("Could not load conditions of trigger (" + triggerID + ") from DB. trigger is: " + e.Message);
                }
            }
            catch(Exception e)
            {
                throw new Exception("Could not load trigger from DB. Info is: " + e.Message);
            }

            return trigger;
        }

        public void updateTriggerStatus(String triggerID, String status)
        {
            DBTrigger dbTrigger = new DBTrigger();
            dbTrigger.TriggerID = triggerID;
            dbTrigger.Status = status;
            ConfuciusDBHelper.updateTriggerStatus(dbTrigger);
        }

        public List<String> retrieveTriggerIDs(String gameID)
        {
            List<String> triggerIDs = new List<String>();
            try
            {
                Uri uri = new Uri("assets/gameFiles/" + gameID + "/gametrigger.xml", UriKind.Relative);
                StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
                XDocument loadedData = XDocument.Load(resourceInfo.Stream);

                var triggers = from record in loadedData.Descendants("gameTrigger")
                               select record;

                foreach (XElement triggerRecord in triggers)
                {
                    String triggerID = (string)triggerRecord.Element("triggerID").Value;
                    if (triggerID != null)
                    {
                        triggerIDs.Add(triggerID);
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("Could not load triggerIDs from DB. Info is: " + e.Message);
            }

            return triggerIDs;

        }
    }
}
